package com.sbc.database.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.sbc.convention.constants.DeleteFlagEnum;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

/**
 * @description: 元数据处理器
 * @author: songbaicheng
 * @create: 2024-03-21 22:39
 **/
public class MyMetaObjectHandler implements MetaObjectHandler {

    /**
     * 数据新增时填充
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "delFlag", Integer.class, DeleteFlagEnum.NORMAL.getStatusCode());
    }

    /**
     * 数据修改时填充
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        // mp 策略：如果属性有值则不覆盖, 如果填充值为null则不填充。
//        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
    }
}
